#region Imported namespaces
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using PHSRAG.Utility;
using PHSRAG.Insight21.Agreements;
#endregion

namespace PHSRAG.Insight21.Common.CommonControls
{
	/// <summary>
	///	Provides a means to get project details of a fund number.
	/// </summary>
	public partial class FundNumberLookup : System.Web.UI.UserControl
	{		
		#region Private Variables
		/// <summary>Policy used for caching business objects.</summary>
		private	Policy.ICachePolicy		objectCachePolicy;
		#endregion

		#region Enums (Private)
		private enum DataGridColumnIndex 
		{
			PIName, SponsorName, ShortTitle, ProjectStartDate, ProjectEndDate, BudgetStartDate, BudgetEndDate
		}

		#endregion

		#region Event Handlers
		/// <summary>
		/// Sets the title for the popup and adds the javascript for clear button.
		/// </summary>
		/// <param name="sender">The object raising the event.</param>
		/// <param name="e">Information about the event itself.</param>
		protected void OnPageLoad(object sender, System.EventArgs e)
		{
			if (!IsPostBack) 
			{
				((HtmlGenericControl)Page.FindControl("pageTitle")).InnerText = "Fund # Lookup";
			}
		}

		/// <summary>
		/// Formats the display of date columns in the grid. It ignores the time portion
		/// of the date.
		/// </summary>
		/// <param name="sender">The object that raised the event.</param>
		/// <param name="e">Information about the event itself.</param>
		private void OnItemDataBound(object sender, DataGridItemEventArgs e)
		{
			try
			{
				if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
				{
					DataRowView rowView = (DataRowView)e.Item.DataItem;

					e.Item.Cells[(int)DataGridColumnIndex.ProjectStartDate].Text = 
						rowView["ProjectStartDate"] != DBNull.Value ? Convert.ToDateTime(rowView["ProjectStartDate"]).ToShortDateString() : string.Empty;
					e.Item.Cells[(int)DataGridColumnIndex.ProjectEndDate].Text = 
						rowView["ProjectEndDate"] != DBNull.Value ? Convert.ToDateTime(rowView["ProjectEndDate"]).ToShortDateString() : string.Empty;
					e.Item.Cells[(int)DataGridColumnIndex.BudgetStartDate].Text = 
						rowView["BudgetStartDate"] != DBNull.Value ? Convert.ToDateTime(rowView["BudgetStartDate"]).ToShortDateString() : string.Empty;
					e.Item.Cells[(int)DataGridColumnIndex.BudgetEndDate].Text = 
						rowView["BudgetEndDate"] != DBNull.Value ? Convert.ToDateTime(rowView["BudgetEndDate"]).ToShortDateString() : string.Empty;
				}
			}
			catch(Exception ex)
			{
				annunciator.Message = String.Format("{0} while attempting to format columns.",Utility.ExceptionReport.GetExceptionInfo(ex, false));
			}

		}

		/// <summary>
		/// Fires when the search button is clicked, at which point in time
		/// the datagrid is loaded with project details for the fund number
		/// entered.
		/// </summary>
		/// <param name="sender">The object raising the event.</param>
		/// <param name="e">Information about the event itself.</param>
		protected void OnSearch(object sender, EventArgs e)
		{
			try 
			{
				LoadDataGrid();
				ManageDataGridVisibility();
			} 
			catch (Exception ex) 
			{
				annunciator.Message = Utility.ExceptionReport.GetExceptionInfo(ex, false);
				Utility.ExceptionReport.WriteToEventLog("FundNumberLookup", ex, true, Utility.ExceptionReport.ReportType.Error);
			}
		}

		/// <summary>
		/// Fires when the clear button is clicked, at which point in time
		/// the datagrid is rendered invisible and annunciator message
		/// set appropriately.
		/// </summary>
		/// <param name="sender">The object raising the event.</param>
		/// <param name="e">Information about the event itself.</param>
		protected void OnClear(object sender, EventArgs e)
		{
			fundNumber.Text = string.Empty;
			dataGrid.Visible = false;
			annunciator.Message = "Please provide fund number then click search.";
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Loads the datagrid with fund project details.
		/// </summary>
		private void LoadDataGrid()
		{
			Fund fund = new Fund(objectCachePolicy);
			fund.Load(Utility.DataAccess.GetConnectionString(),
				fundNumber.Text.Trim(), FundCommandType.GetFundDetails);
			
			dataGrid.DataSource = fund.GetDataContainer();
			dataGrid.DataBind();
		}

		/// <summary>
		/// Makes the datagrid invisible if the resultset is empty (visible otherwise) 
		/// and sets the annunciator message appropriately.
		/// </summary>
		private void ManageDataGridVisibility() 
		{
			if (dataGrid.Items.Count == 0) 
			{
				dataGrid.Visible = false;
				annunciator.Message = "No project details found for fund number entered.";
			}
			else
			{
				dataGrid.Visible = true;
				annunciator.Message = string.Empty;
			}
		}
		#endregion

		#region Web Form Designer generated code
		/// <summary>
		/// Handles wiring up of page events.
		/// </summary>
		/// <param name="e">Information about the event itself.</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);

			objectCachePolicy = (Policy.ICachePolicy)Session[Policy.Names.CachePolicyKey];

			ID = "fundNumberLookup";
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.OnPageLoad);
			dataGrid.ItemDataBound +=new DataGridItemEventHandler(OnItemDataBound);

		}
		#endregion
	}
}
